Program Flow of ATmega16 microcontroller
/* Name : Main.c * Purpose : Source code for keypad Interfacing with ATMEGA16. * Author : GEMICATES * Date : 04-07-2017 * Website : www.gemicates.org * Revision : None */ #include<avr/io.h> // Header File for ATMEGA16 #include<util/delay.h> // Include Delay Function #define KEY PORTB // To Set Port B as Keypad #define r1 PB0 // To Set Pin B0-B3 as Keypad Row #define r2 PB1 #define r3 PB2 #define r4 PB3 #define c1 PB4 // To Set Pin B4-B6 as Keypad Column #define c2 PB5 #define c3 PB6 /* Function to Check Four Rows and Columns*/ void check1(void); void check2(void); void check3(void); void check4(void); #define LCD_DATA PORTD // To Set Port D as LCD data Pins #define CON PORTA // To Set Port A Pins A0-A2 as LCD Control Pins #define en PA2 // Enable signal #define rw PA1 // Read/Write signal #define rs PA0 // Resister Select signal /* Function For LCD display*/ void LCD_cmd(unsigned char cmd); void init_LCD(void); void LCD_write(unsigned char data); void string(unsigned char *str); /* Main Function*/ void main() { unsigned char value; DDRD=0xff; // LCD_DATA port as output port DDRA=0x07; // Set the Data Direction Register (A2-A0) as Ouput DDRB=0x0F; // Set the Data Direction Register (B3-B0) as Ouput KEY=0xf0; // Make Input Pins are High init_LCD(); // initialization of LCD LCD_cmd(0x86); // bring cursor to position 6 of ROW 1 LCD_write('H'); LCD_cmd(0x87); // bring cursor to position 7 of ROW 1 LCD_write('I'); LCD_write('!'); _delay_ms(50); LCD_cmd(0xC3); // bring cursor to position 3 of ROW 2 string("**GUYS**"); _delay_ms(50); LCD_cmd(0x01); // clear screen LCD_cmd(0x83); // bring cursor to position 3 of ROW 1 string("WELCOME TO"); _delay_ms(100); LCD_cmd(0xC3); // bring cursor to position 3 of ROW 2 string("GEMICATES"); _delay_ms(100); LCD_cmd(0x01); // clear screen LCD_cmd(0x0C); // Display On cursor Off string("press a key"); LCD_cmd(0xc0); while(1) { PORTB=0xF0; // Set all the input to one value=PINB; // Get the PORTD value in variable "value" if(value!=0xf0) // If any key is pressed value changed { check1(); check2(); check3(); check4(); } } return 0; } void check1(void) // To check Row1 { KEY =0b11111110; _delay_ms(20); if(bit_is_clear(PINB,c1)) { output(1); _delay_ms(20); } else if(bit_is_clear(PINB,c2)) { output(2); _delay_ms(20); } else if(bit_is_clear(PINB,c3)) { output(3); _delay_ms(20); } } void check2(void) // TO check Row2 { KEY=0b11111101; _delay_ms(20); if(bit_is_clear(PINB,c1)) { output(4); _delay_ms(20); } else if(bit_is_clear(PINB,c2)) { output(5); _delay_ms(20); } else if(bit_is_clear(PINB,c3)) { output(6); _delay_ms(20); } } void check3(void) // To check Row3 { KEY=0b11111011; _delay_ms(20); if(bit_is_clear(PINB,c1)) { output(7); _delay_ms(20); } else if(bit_is_clear(PINB,c2)) { output(8); _delay_ms(20); } else if(bit_is_clear(PINB,c3)) { output(9); _delay_ms(20); } } void check4(void) // To check Row4 { KEY =0b11110111; _delay_ms(20); if(bit_is_clear(PINB,c1)) { output(10); _delay_ms(20); } else if(bit_is_clear(PINB,c2)) { output(11); _delay_ms(20); } else if(bit_is_clear(PINB,c3)) { output(12); _delay_ms(20); } } void init_LCD(void) { LCD_cmd(0x38); // initializtion of 16X2 LCD in 8bit mode _delay_ms(1); LCD_cmd(0x01); // clear LCD _delay_ms(1); LCD_cmd(0x0E); // cursor ON _delay_ms(1); LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position _delay_ms(1); return; } void LCD_cmd(unsigned char cmd) { LCD_DATA=cmd; CON =(0<<rs)|(0<<rw)|(1<<en); // making RS and RW as LOW and EN as HIGH _delay_ms(1); CON =(0<<rs)|(0<<rw)|(0<<en); // making RS, RW , LOW and EN as LOW _delay_ms(50); return; } void LCD_write(unsigned char data) { LCD_DATA= data; CON = (1<<rs)|(0<<rw)|(1<<en); // making RW as LOW and RS, EN as HIGH _delay_ms(1); CON = (1<<rs)|(0<<rw)|(0<<en); // making EN and RW as LOW and RS HIGH _delay_ms(50); // give a 10 milli second delay to get thigs executed return ; } void string(unsigned char *str) // take address vaue of the string in pionter *str { int i=0; while(str[i]!='\0') // loop will go on till the NULL charaters is soon in string { LCD_write(str[i]); // sending data on CD byte by byte i++; } return; } void output(int rev) { LCD_cmd(0x01); // clear screen LCD_cmd(0x86); // bring cursor to position 3 of ROW 1 switch(rev) { case 1: string("ONE"); _delay_ms(1000); break; case 2: string("TWO"); _delay_ms(1000); break; case 3: string("THREE"); _delay_ms(1000); break; case 4: string("FOUR"); _delay_ms(1000); break; case 5: string("FIVE"); _delay_ms(1000); break; case 6: string("SIX"); _delay_ms(1000); break; case 7: string("SEVEN"); _delay_ms(1000); break; case 8: string("EIGHT"); _delay_ms(1000); break; case 9: string("NINE"); _delay_ms(1000); break; case 10: string("TEN"); _delay_ms(1000); break; case 11: string("ELEVEN"); _delay_ms(1000); break; case 12: string("TWELVE"); _delay_ms(1000); break; } }